package validate

import (
	"china-minzu-server/model"
	"china-minzu-server/service"
	"errors"
	"fmt"
	"regexp"
)

func IsValid4sysUser(u model.User) error {
	reUsername := regexp.MustCompile("^.{4,16}$")
	if !reUsername.MatchString(u.UserName) {

		fmt.Println("用户名不合法,长度为4-16位")
		return errors.New("用户名不合法,长度为4-16位")
	}
	rePassword := regexp.MustCompile("^.{6,18}$")
	if !rePassword.MatchString(u.UserPassword) {
		fmt.Println("密码不合法,长度为6-18位")
		return errors.New("密码不合法,长度为6-18位")

	}
	//reNickName := regexp.MustCompile("^.{2,20}$")
	//if !reNickName.MatchString(u.UserName) {
	//	return errors.New("昵称不合法,长度为2-20位")
	//}
	rePhone := regexp.MustCompile("^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\\d{8}$")
	if u.UserTel != "" && !rePhone.MatchString(u.UserTel) {


		return errors.New("手机号不合法")

	}
	if service.IsTelphoneExist(u.UserTel) != nil{
		return nil
	}
	return errors.New("已经有该手机号")

	reEmail := regexp.MustCompile("^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$")
	if u.UserEMmail != "" && !reEmail.MatchString(u.UserEMmail) {

		fmt.Println("邮箱不合法")
		return errors.New("邮箱不合法")

	}
	return nil
}
